/* Copyright (c) 2021, National University of Defense Technology. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "vec_axpby.h"
#include "vec_axpbypcz.h"

namespace YHAMG
{

void Vec_AXPBYPCZ(double alpha, const Vector& x, double beta, const Vector& y, double gamma, const Vector& z, const Vector& w)
{
	if (alpha == 0)
	{
		Vec_AXPBY(beta, y, gamma, z, w);
		return;
	}

	if (beta == 0)
	{
		Vec_AXPBY(alpha, x, gamma, z, w);
		return;
	}

	if (gamma == 0)
	{
		Vec_AXPBY(alpha, x, beta, y, w);
		return;
	}

	int n = w.size;
	double* xv = x.values;
	double* yv = y.values;
	double* zv = z.values;
	double* wv = w.values;

	if (alpha == 1)
	{
#ifdef USE_OPENMP
#pragma omp parallel for
#endif
		for (int i = 0; i < n; ++i)
			wv[i] = beta * yv[i] + gamma * zv[i] + xv[i];
	}
	else if (alpha == -1)
	{
#ifdef USE_OPENMP
#pragma omp parallel for
#endif
		for (int i = 0; i < n; ++i)
			wv[i] = beta * yv[i] + gamma * zv[i] - xv[i];
	}
	else if (beta == 1)
	{
#ifdef USE_OPENMP
#pragma omp parallel for
#endif
		for (int i = 0; i < n; ++i)
			wv[i] = alpha * xv[i] + gamma * zv[i] + yv[i];
	}
	else if (beta == -1)
	{
#ifdef USE_OPENMP
#pragma omp parallel for
#endif
		for (int i = 0; i < n; ++i)
			wv[i] = alpha * xv[i] + gamma * zv[i] - yv[i];
	}
	else if (gamma == 1)
	{
#ifdef USE_OPENMP
#pragma omp parallel for
#endif
		for (int i = 0; i < n; ++i)
			wv[i] = alpha * xv[i] + beta * yv[i] + zv[i];
	}
	else if (gamma == -1)
	{
#ifdef USE_OPENMP
#pragma omp parallel for
#endif
		for (int i = 0; i < n; ++i)
			wv[i] = alpha * xv[i] + beta * yv[i] - zv[i];
	}
	else
	{
#ifdef USE_OPENMP
#pragma omp parallel for
#endif
		for (int i = 0; i < n; ++i)
			wv[i] = alpha * xv[i] + beta * yv[i] + gamma * zv[i];
	}
}

}